package com.xckj.teacher.ui.activity

import android.view.View
import androidx.recyclerview.widget.GridLayoutManager
import com.blankj.utilcode.util.GsonUtils
import com.blankj.utilcode.util.LogUtils
import com.blankj.utilcode.util.ToastUtils
import com.xckj.teacher.R
import com.xckj.teacher.bean.Group
import com.xckj.teacher.databinding.ActivityGroupBinding
import com.xckj.teacher.framework.BaseActivity
import com.xckj.teacher.presenter.ClassPresenter
import com.xckj.teacher.ui.adapter.GroupAdapter
import com.xckj.teacher.ui.fragment.DeleteDialog
import com.xckj.teacher.util.ActivityMessenger
import com.xckj.teacher.util.get


/**
 * 分组
 */
class GroupActivity : BaseActivity<ActivityGroupBinding>() {
    private val presenter: ClassPresenter by lazy { ClassPresenter(this) }
    private val groupAdapter: GroupAdapter by lazy {
        GroupAdapter(arrayListOf())
    }
    var groupData: Group? = null
    var classId = ""

    inner class ProxyClick {
        fun back() {
            finish()
        }

        fun submit() {
            var ids = ""
            groupAdapter.data.forEach {
                if (it.check) {
                    if (ids == "") {
                        ids = it.id.toString()
                    } else {
                        ids = "$ids,${it.id}"
                    }
                }
            }
            if (ids == "") {
                ToastUtils.showShort("请选择")
                return
            }
            if (dataBinding.etName.text.trim().toString().isNullOrEmpty()) {
                ToastUtils.showShort("请输入小组名称")
                return
            }

            presenter.addGroup(
                ids,
                dataBinding.etName.text.trim().toString(),
                classId
            ) {
                ToastUtils.showShort("添加分组成功")
                ActivityMessenger.finish(this@GroupActivity, "data" to "refresh")
            }
        }

        fun delete() {
            DeleteDialog(groupData?.name ?: "") {
                presenter.deleteGroup(groupData?.id.toString()) {
                    ToastUtils.showShort("删除分组成功")
                    ActivityMessenger.finish(this@GroupActivity, "data" to "refresh")
                }
            }.show(supportFragmentManager, "")

        }


    }

    override fun loadLayout(): Int {
        return R.layout.activity_group
    }

    override fun initData() {
        dataBinding.click = ProxyClick()
        val groupJson = intent.get<String>("data")
        classId = intent.get<String>("classId") ?: ""
        groupData = GsonUtils.fromJson(groupJson, Group::class.java)
        dataBinding.data = groupData
        var toString = groupData?.id
        if (groupData?.id == null) {
            dataBinding.tvTitle.text = "新建分组"
            dataBinding.etName.setText("")
            dataBinding.tvDelete.visibility = View.GONE
        } else {
            dataBinding.tvTitle.text = "编辑分组"
            dataBinding.etName.setText("${groupData?.name}")
            dataBinding.tvDelete.visibility = View.VISIBLE
            groupData?.student?.forEach {
                it.check = true
            }
            dataBinding.cb.isChecked = true
            dataBinding.tvSubmit.text = "完成 (已选${groupData?.student?.size}个学生)"
        }
        LogUtils.e(toString == null)
        dataBinding.rv.layoutManager = GridLayoutManager(this, 3)
        dataBinding.rv.adapter = groupAdapter
        groupAdapter.setList(groupData?.student)
    }

    override fun setListener() {
        groupAdapter.setOnItemClickListener { adapter, view, position ->
            groupAdapter.data[position].check = !groupAdapter.data[position].check
            groupAdapter.notifyDataSetChanged()
            var filter = groupAdapter.data.filter {
                it.check
            }
            dataBinding.cb.isChecked = filter.size == groupAdapter.data.size
            dataBinding.tvSubmit.text = "完成 (已选${filter.size}个学生)"
        }
        dataBinding.cb.setOnClickListener {
            groupAdapter.data.forEach {
                it.check = dataBinding.cb.isChecked
            }
            if (dataBinding.cb.isChecked) {
                dataBinding.tvSubmit.text = "完成 (已选${groupAdapter.data.size}个学生)"
            } else {
                dataBinding.tvSubmit.text = "完成 (已选0个学生)"
            }
            groupAdapter.notifyDataSetChanged()
        }
    }

}